import {Component, OnInit} from '@angular/core';
import {AfterViewInit, ViewChild} from '@angular/core';

import {ArticleService} from "../service/article.service";
import {ArticleEditorComponent} from "./articleEditor.component";
import {ArticleArea} from "../entity/articleArea";

@Component({
    moduleId: module.id,
    selector: 'my-articleUpload',
    templateUrl: 'articleUpload.component.html',
    styleUrls: ['articleUpload.component.css'],
    providers: [ArticleService]
})

export class ArticleUploadComponent implements OnInit,AfterViewInit {

    @ViewChild(ArticleEditorComponent)
    private articleEditorComponent: ArticleEditorComponent;

    ngAfterViewInit() {
        this.articleEditorComponent.editor.create();
    }

    constructor(private articleService: ArticleService) {
    }

    public articleTitle: string;
    public articleSynopsis: string;
    public articleContent: string;
    public articleArea: string;
    articlesAreas: ArticleArea[];
    errorMessage: string;

    uploadArticle(): void {
        this.articleContent = this.articleEditorComponent.editor.$txt.html();
        this.articleService.insertArticle(this.articleArea, this.articleTitle, "me", this.articleSynopsis, this.articleContent);
        console.log(this.articleTitle);
        console.log(this.articleSynopsis);
        console.log(this.articleArea);
        console.log(this.articleContent);
    }

    ngOnInit(): void {
        this.articleService.getArticlesAreas().subscribe(articlesAreas => this.articlesAreas = articlesAreas, error => this.errorMessage = <any>error);
    }
}